package com.koron.css2.clientCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.clientCenter.bean.ValveCloseCountBean;
import com.koron.css2.clientCenter.mapper.ValveCloseDetailMapper;
import com.koron.css2.clientCenter.mapper.ValveCloseMapper;
import com.koron.css2.clientCenter.queryBean.ValveCloseListQueryBean;
import com.koron.css2.clientCenter.vo.ValveCloseVo;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.BeanUtils;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 表阀门关闭 管理列表
 *
 * @author cd
 * @author modify by 杨嘉慧
 */
public class ValveCloseList implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            @SuppressWarnings("rawtypes")
            MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);

            ValveCloseListQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), ValveCloseListQueryBean.class);
            ValveCloseMapper valveCloseMapper = factory.getMapper(ValveCloseMapper.class);

            PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
            List<ValveCloseVo> valveCloseList = valveCloseMapper.selectList(queryBean);

            if (null == valveCloseList || valveCloseList.size() == 0) {
                info.setData(new PageInfo());
                return info;
            }
            //批量count 出已开表数、已开表数等相关数据
            List<String> ids = valveCloseList.stream().map(ValveCloseVo::getId).collect(Collectors.toList());

            ValveCloseDetailMapper detailMapper = factory.getMapper(ValveCloseDetailMapper.class);
            //根据主表id查出主表数据关联的明细统计数据
            List<ValveCloseCountBean> countList = detailMapper.countByValveIds(ids);
            //将计数list转换map
//            Map<String, ValveCloseCountBean> countMap = countList.stream().collect(Collectors.toMap(ValveCloseCountBean::getValveCloseId, valveCloseCountBean -> valveCloseCountBean));
            Map<String, ValveCloseCountBean> countMap = new HashMap<>();
            for (ValveCloseCountBean countBean : countList) {
                //处理待开阀数 待开阀数=开阀总数-已开数
                countBean.setFinishNum(countBean.getCloseNum() + countBean.getCancelNum());
                countBean.setWaitOpenNum(countBean.getOpenSum() - countBean.getOpenNum());
                countMap.put(countBean.getValveCloseId(), countBean);
            }

            //两个对象list 合并到新对象list
            for (ValveCloseVo vo : valveCloseList) {
                if (countMap.containsKey(vo.getId())) {
                    BeanUtils.copyProperties(countMap.get(vo.getId()), vo);
                }
                if (vo.getCloseNum() + vo.getCancelNum() == vo.getPlanCloseNum()) {
                    vo.setValveCloseStatus("1");
                    vo.setValveCloseName("完成");
                } else {
                    vo.setValveCloseStatus("2");
                    vo.setValveCloseName("未完成");
                }
                if (!StringUtils.isEmpty(vo.getBusinessArea())) {
                    BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
                    vo.setBusinessAreaName(
                            businessAreaMapper.findBusinessAreaById(vo.getBusinessArea(), userInfo.getCurWaterCode()));
                }
                vo.setProcessName(Tools.transProcessStatus(vo.getProcessState()));
            }
            info.setData(new PageInfo<>(valveCloseList));
            return info;
        } catch (Exception e) {
            logger.error("关阀管理列表查询失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
        }
    }


}
